<?
$sub_menu = "230540";
include_once("./_common.php");

auth_check($auth[$sub_menu], "r");

$g4[title] = "가입경로별 통계";
include_once("$g4[admin_path]/admin.head.php");

if (empty($fr_date)) // 시작날짜 초기화
    $fr_date = date("Y-m-01", $g4[server_time]);

if (empty($to_date)) // 종료날짜 초기화
    $to_date = date("Y-m-t", $g4[server_time]);

$qstr = "fr_date=$fr_date&to_date=$to_date";

$colspan = 6;
?>

<div style="margin:0 0 10px 0; padding:10px; border:1px solid #ddd;">
배추 파워팩을 설치하셔야 통계가 보입니다.
</div>


<script type="text/javascript">
function fcount_submit(ymd, sdate, edate) 
{
    var f = document.fcount;
    f.ymd.value = ymd;
    if (sdate) f.fr_date.value = sdate;
    if (edate) f.to_date.value = edate;
    f.action = "<?=$PHP_SELF?>";
    f.submit();
}
</script>

<form name=fcount method=get style="margin:0;">
<input type=hidden name="ymd" value="d">
<table width=100% cellpadding=3 cellspacing=1>
<tr>
    <td>
        기간 : 
        <input type='text' name='fr_date' size=11 maxlength=10 value='<?=$fr_date?>' class=ed>
        -
        <input type='text' name='to_date' size=11 maxlength=10 value='<?=$to_date?>' class=ed>
        &nbsp;
        <input type=submit class=btn1 value=' 검색 '>
        <input type=button class=btn1 value=' 이번달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", $g4[server_time])?>', '<?=date("Y-m-t", $g4[server_time])?>');">
        <input type=button class=btn1 value=' 지난달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?=date("Y-m-t", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 다음달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?=date("Y-m-t", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 상반기 '
            onclick="fcount_submit('m', '<?=date("Y-01-01")?>', '<?=date("Y-06-30")?>');">
        <input type=button class=btn1 value=' 하반기 '
            onclick="fcount_submit('m', '<?=date("Y-07-01")?>', '<?=date("Y-12-31")?>');">
        <input type=button class=btn1 value=' 올해 '
            onclick="fcount_submit('m', '<?=date("Y-01-01")?>', '<?=date("Y-12-31")?>');">

        <input type=button class=btn1 value=' 전체 ' onclick="fcount_submit('d', '2000-01-01', '<?=date("Y-m-t", $g4[server_time])?>')">
    </td>
</tr>
</table>
</form>

<table width=100% cellpadding=0 cellspacing=1 border=0>
<colgroup width=100>
<colgroup width=200>
<colgroup width=100>
<colgroup width=100>
<colgroup width=100>
<colgroup width=''>
<tr><td colspan='<?=$colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td>순위</td>
    <td>접속 도메인</td>
    <td>가입자수</td>
    <td>결제금액</td>
    <td>비율(%)</td>
    <td>그래프</td>
</tr> 
<tr><td colspan='<?=$colspan?>' class='line2'></td></tr>
<?
$max = 0;
$max2 = 0;
$arr = array();
$arr2 = array();
$sum_count = 0;
$sum_pay = 0;
$sql = " select * from $g4[member_table] as m, $mw_cash[cash_list_table] as c
          where left(m.mb_datetime,10) between '$fr_date' and '$to_date'
            and m.mb_id = c.mb_id 
            and c.cl_cash > 0
            and (c.cl_paytype <> '포인트' and c.cl_paytype <> '서비스' and c.cl_paytype <> '페이팔')
            and c.cl_status = '1'
";
$result = sql_query($sql);
while ($row=sql_fetch_array($result)) {
    $str = $row[mb_referer];
    preg_match("/^http[s]*:\/\/([\.\-\_0-9a-zA-Z]*)\//", $str, $match);
    $s = $match[1];
    $s = preg_replace("/^(www\.|search\.|dirsearch\.|dir\.search\.|dir\.|kr\.search\.|myhome\.)(.*)/", "\\2", $s);
    $arr[$s]++;
    $arr2[$s] += $row[cl_payment];

    if ($arr[$s] > $max) $max = $arr[$s];
    if ($arr2[$s] > $max2) $max2 = $arr2[$s];

    $sum_count++;
    $sum_pay += $row[cl_payment];
}

$i = 0;
$k = 0;
$save_count = -1;
$tot_count = 0;
if (count($arr)) {
    arsort($arr);
    foreach ($arr as $key=>$value) {
        $count = $arr[$key];
        $pay = $arr2[$key];
        if ($save_pay != $pay) {
            $i++;
            $no = $i;
            $save_pay = $pay;
        } else {
            $no = "";
        }

        if (!$key) {
            $link = "";
            $key = "직접"; 
        } else {
            $link = "<a href='$g4[admin_path]/mw.cash/mw.cash.list.php?sfl=mb_referer&stx=$key' title='상세보기'>";
        }

        $rate = @($pay / $sum_pay * 100);
        $s_rate = number_format($rate, 1);

        $bar = (int)@($pay / $max2 * 100);
        $graph = "<img src='{$g4[admin_path]}/img/graph.gif' width='$bar%' height='18'>";

        $list = ($k++%2);
        echo "
        <tr class='list$list ht center'>
            <td>$no</td>
            <td align=left>$link$key</a></td>
            <td>$count</td>
            <td>".number_format($pay)."</td>
            <td>$s_rate</td>
            <td align=left>$graph</td>
        </tr>";
    }

    echo "
    <tr><td colspan='$colspan' class='line2'></td></tr>
    <tr class='bgcol2 bold col1 ht center'>
        <td colspan=2>합계</td>
        <td>$sum_count</td>
        <td>".number_format($sum_pay)."</td>
        <td colspan=2>&nbsp;</td>
    </tr>";
} else {
    echo "<tr><td colspan='$colspan' height=100 align=center>자료가 없습니다.</td></tr>";
}
?>
<tr><td colspan='<?=$colspan?>' class='line2'></td></tr>
</table>

<?
include_once("$g4[admin_path]/admin.tail.php");
?>
